package com.cxyd.modules.api.controller;

import com.cxyd.common.utils.Result;
import com.cxyd.common.utils.ApiResult;
import com.cxyd.modules.api.dto.CallbackDTO;
import com.cxyd.modules.api.dto.RmsNoticeDTO;
import com.cxyd.modules.api.dto.StockDetailDTO;
import com.cxyd.modules.api.dto.StockParamsDTO;
import com.cxyd.modules.api.service.CallbackService;
import com.cxyd.modules.api.service.RmsNoticeService;
import com.cxyd.modules.wms.dto.InterfaceMesReqDTO;
import com.cxyd.modules.wms.service.OrderService;
import com.cxyd.modules.wms.service.StockService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class ReceiveApiController {
    private static final Logger logger = LoggerFactory.getLogger(ReceiveApiController.class);
    @Resource
    private CallbackService receiveWcsService;
    @Resource
    private RmsNoticeService rmsNoticeService;
    @Resource
    private OrderService orderService;
    @Resource
    private StockService stockService;

    /**
     * 连接测试
     */
    @GetMapping("ping")
    public ApiResult ping() {
        return new ApiResult().success("当前时间:" + LocalTime.now());
    }

    @PostMapping("/wcs/callback")
    public ApiResult wcsCallBackNew(@RequestBody CallbackDTO dto) {
        int code = 0;
        String msg = "成功";
        //数据校验
        final String requestPk = dto.getRequestPk();
        if (StringUtils.isBlank(requestPk)) {
            return new ApiResult().feedBack("requestPk不能为空!", "404");
        }
        if (StringUtils.isBlank(dto.getCurPos())) {
            return new ApiResult().feedBack("当前位置不能为空!", requestPk);
        }
        if (StringUtils.isBlank(dto.getNoticeType())) {
            return new ApiResult().feedBack("任务类型不能为空!", requestPk);
        }
        try {
            receiveWcsService.callBackProcess(dto);
        } catch (Exception e) {
            logger.info("wcs回调任务失败", e);
            code = -1;
            msg = e.getMessage();
        }
        //反馈成功
        return new ApiResult().feedBack(String.valueOf(code), msg, requestPk);
    }

    @PostMapping("/ApiAgvForWms/WeightInfo")
    public ApiResult rmsNotice(@RequestBody RmsNoticeDTO dto) {
        logger.info("接收到rms系统称重通知:" + dto);
        rmsNoticeService.noticeProcess(dto);
        return new ApiResult().success("success");
    }
}
